<?php

/**
 * Subclass for representing a row from the 'kandidatai' table.
 *
 *
 *
 * @package lib.model
 */
/**
 * Kandidatai
 *
 * @package
 * @author Ramunas
 * @copyright Copyright (c) 2010
 * @version $Id$
 * @access public
 */
class Kandidatai extends BaseKandidatai
{
	/**
	 * Faq::hydrate()
	 * Sets culture to result set retrieved from database in order not to set it everytime when writing new method
	 * @param ResultSet $rs
	 * @param integer $startcol
	 *
	 */
	public function hydrate(ResultSet $rs, $startcol = 1)
	{
		$this->setCulture(sfContext::getInstance()->getUser()->getCulture());

		parent::hydrate($rs, $startcol);
	}
	/**
	 * Faq::getNameLng()
	 * Retrieves object name from database depending on user culture
	 * @return translated Name
	 */
    public function getNameLng()
    {
        $this->setCulture(sfContext::getInstance()->getUser()->getCulture());
        return $this->getName();
    }

	/**
	 * Kandidatai::getPicture()
	 * Returns unique name of picture assigned to current object
	 * @return false if file is not assigned, otherwise - uniquen name of picture
	 */
	public function getPicture()
	{
		$c = new Criteria();
		$c->add(FilesPeer::OBJ_ID, $this->getId());
		$c->add(FilesPeer::OBJ_TYPE, 'kandidatai');
		$pic = FilesPeer::doSelectOne($c);
		if( !$pic )
		{
		    return false;
		} else {
		    return $pic->getUname();
		}

	}


	/**
	 * Kandidatai::getSlugName()
	 * splits objects name into two parts and slugifies each of them - removes all non-latin characters and decapitalizes
	 * @return result in format "/slugifiedPart1/slugifiedPart2/"
	 */
	public function getSlugName()
	{
		$ar = explode(' ', $this->getName());

		if( count($ar) != 2 )
		{
		    return '/';
		}
		else
		{

			// replace non letter or digits by -
			$ar[0] = preg_replace('~[^\\pL\d]+~u', '-', $ar[0]);
			$ar[1] = preg_replace('~[^\\pL\d]+~u', '-', $ar[1]);

			// trim
			$ar[0] = trim($ar[0], '-');
			$ar[1] = trim($ar[1], '-');

			$ar[0] = str_replace(array('ą', 'č', 'ę', 'ė', 'į', 'š', 'ų', 'ū', 'ž', 'Ą', 'Č', 'Ę', 'Ė', 'Į', 'Š', 'Ų', 'Ū', 'Ž'), array('a', 'c', 'e', 'e', 'i', 's', 'u', 'u', 'z', 'A', 'C', 'E', 'E', 'I', 'S', 'U', 'U', 'Z'), $ar[0]);
			$ar[1] = str_replace(array('ą', 'č', 'ę', 'ė', 'į', 'š', 'ų', 'ū', 'ž', 'Ą', 'Č', 'Ę', 'Ė', 'Į', 'Š', 'Ų', 'Ū', 'Ž'), array('a', 'c', 'e', 'e', 'i', 's', 'u', 'u', 'z', 'A', 'C', 'E', 'E', 'I', 'S', 'U', 'U', 'Z'), $ar[1]);

			// transliterate
			$ar[0] = iconv('utf-8', 'us-ascii//TRANSLIT', $ar[0]);
			$ar[1] = iconv('utf-8', 'us-ascii//TRANSLIT', $ar[1]);

			// lowercase
			$ar[0] = strtolower($ar[0]);
			$ar[1] = strtolower($ar[1]);

			// remove unwanted characters
			$ar[0] = preg_replace('~[^-\w]+~', '', $ar[0]);
			$ar[1] = preg_replace('~[^-\w]+~', '', $ar[1]);

			if (empty($ar[0]) || empty($ar[1]))
			{
				//echo 'in';
				return 'n-a';
			}
			return '/' . $ar[0] . '/' . $ar[1] . '/';

			//return $text;
		}

	}
}